package com.wgj.android.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jiaai.xui.utils.XEmptyUtils;
import com.jiaai.xui.utils.http.HttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.R;
import com.wgj.android.entity.Member;
import com.wgj.android.http.WGJAPI;
import com.wgj.android.http.model.AccessToken;
import com.wgj.android.utils.CacheUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FindPwdActivity extends AppCompatActivity {
    @BindView(R.id.phoneEdit)
    EditText phoneEdit;

    @BindView(R.id.pwdEdit)
    EditText pwdEdit;

    @BindView(R.id.code)
    EditText codeEdit;

    @BindView(R.id.reset)
    TextView reset;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_find_pwd);
        ButterKnife.bind(this);
    }
    @OnClick(R.id.button)
    public void  onLogin(final View view){
        String phone=phoneEdit.getText().toString();
        String pwd=pwdEdit.getText().toString();
        String code=codeEdit.getText().toString();
        if(XEmptyUtils.isSpace(phone)){
            XToast.error("请输入手机号码");
            return;
        }
        if(XEmptyUtils.isSpace(pwd)){
            XToast.error("密码不能为空");
            return;
        }
        if(XEmptyUtils.isSpace(code)){
            XToast.error("验证码不能为空");
            return;
        }
        XHttp.obtain().get(WGJAPI.VALIDATA_URL + "?phone=" + phone+"&code="+code, null, new HttpCallBack<String>() {
            @Override
            public void onSuccess(String s) {
                if(s.equalsIgnoreCase("验证成功")){
                    update();
                }else {
                    XToast.error("验证码错误");
                }

            }

            @Override
            public void onFailed(int i, String s, boolean b) {

            }
        });



    }

    private void update() {
        final String pwd=pwdEdit.getText().toString();
        final String phone=phoneEdit.getText().toString();
        Map<String,Object> params=new HashMap<>();
        params.put("mPhone",phone);
        params.put("mPassword",pwd);
        XHttp.obtain().post(WGJAPI.UPDATE_MEMBER_API, params, new HttpCallBack<String>() {
            @Override
            public void onSuccess(String s) {
                XToast.success(s);
                Intent intent = new Intent();
                intent.putExtra("phone",phone);
                setResult(RESULT_OK,intent);
                finish();

            }

            @Override
            public void onFailed(int i, String s, boolean b) {

            }
        });
    }

    @OnClick(R.id.reset)
    public void resetPassword(View view){
        String phone=phoneEdit.getText().toString();
        Map<String,Object> params=new HashMap<>();
        params.put("phone",phone);
        XHttp.obtain().post(WGJAPI.RESETCODE_URL, params, new HttpCallBack<String>() {
            @Override
            public void onSuccess(String s) {
                if(s.equals("false")){
                    XToast.error("请检查你的帐号是否注册");
                }else {
                    new CountDownTimerUtils(60000, 1000).start();
                }
            }

            @Override
            public void onFailed(int i, String s, boolean b) {
                XToast.error("请检查你的帐号是否正确");
            }
        });
    }
    public class CountDownTimerUtils extends CountDownTimer {


        public CountDownTimerUtils(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);

        }

        @Override
        public void onTick(long millisUntilFinished) {
            // sendCode.setText("重新发送("+millisUntilFinished / 1000+")");  //设置倒计时时间
            reset.setClickable(false); //设置不可点击
            reset.setText(millisUntilFinished / 1000 + "秒后可重新发送");  //设置倒计时时间

            SpannableString spannableString = new SpannableString(reset.getText().toString());  //获取按钮上的文字
            ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
            spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//将倒计时的时间设置为红色
            reset.setText(spannableString);
        }


        @Override
        public void onFinish() {
            reset.setText("获取验证码");
            //reset.setTextColor(XThemeUtils.getColorPrimary(LoginActivity.this));
            reset.setClickable(true);//重新获得点击
        }
    }
}
